package ch.kerbtier.webglue;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class GlueSessionListener implements HttpSessionListener{

	public void sessionCreated(HttpSessionEvent hse) {
		ServletContext sc = hse.getSession().getServletContext();
		final CodeRunner codeRunner = (CodeRunner)sc.getAttribute("glue_code_runner");
		Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("servletContext", sc);
        parameters.put("context", sc);
        parameters.put("session", hse.getSession());
        
        codeRunner.run("sessionCreated.groovy", parameters);
        
	}

	public void sessionDestroyed(HttpSessionEvent hse) {
		ServletContext sc = hse.getSession().getServletContext();
		final CodeRunner codeRunner = (CodeRunner)sc.getAttribute("glue_code_runner");
		Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("servletContext", sc);
        parameters.put("context", sc);
        parameters.put("session", hse.getSession());
        
        codeRunner.run("sessionDestroyed.groovy", parameters);

	}
}
